#!usr/bin/python
# -*- coding: utf-8 -*-
print('对mongodb增加字段')

import xlrd
import pymongo
import pandas as pd
import re

org=pd.read_excel("C:\\Users\\Administrator\\Desktop\\orgs.xlsx",encoding='gbk')
columns=['英文','中文']
orgs=pd.DataFrame(org,columns=columns)
print(orgs)

connection=pymongo.MongoClient('mongodb://192.168.1.123:27017')
db=connection.test
collection=db.aminer_authors
print('数据库连接成功')

for index,row in orgs.iterrows():
    regex=".*"+row['英文']+".*"
    cursor=collection.find({"orgs":re.compile(regex,re.IGNORECASE)})
    # 查询考虑: 区分大小写,且orgs字段必须包含xlsx表中的英文;包含一部分英文,匹配不成功.
    if (cursor is not None):
        for document in cursor:
            db.aminer_authors.update_one({'_id':document['_id']},{'$set':{'chiorgs':row['中文']}})   # 对数据库进行更新

cursor=collection.find({'chiorgs':{'$exists':False}})
for document in cursor:
    db.aminer_authors.update_one({'_id':document['_id']},{'$set':{'chiorgs':""}})